﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI;
using ZhongheCms.Models;
using GsnHelper;
namespace ZhongheCms.Controllers
{
    public class SunJobController : Controller
    {
        //职位列表
        private ZhongheRepo db = new ZhongheRepo();
        public ActionResult JobList(int id = 0, string p1 = "", string p2 = "", string p3 = "", string p4 = "", string p5 = "", int pageIndex = 1)
        {
            //p1 职位类别 59
            //p2 工作类型 60
            //p3 结算方式 66
            //p4 性别 67
            //p5 身高 68
           
            //var qry = db.Page<GswTblJobInfo>(pageIndex, 20, strSql);
            var sql = "select * from GswTblJobInfo where ParentId=19 and JobState=1 and delflag=0 and RegisterNum<JobNum";
            if (InputValid.StrToInt(p1, 0) > 0)
                sql += " and  JobTypeId=" + p1;
            if (InputValid.StrToInt(p2, 0) > 0)
                sql += " and  WorkTypeId=" + p2;
            if (InputValid.StrToInt(p3, 0) > 0)
                sql += " and   PayTypeId=" + p3;
            if (InputValid.StrToInt(p4, 0) > 0)
                sql += " and   GenderId=" + p4;
            if (InputValid.StrToInt(p5, 0) > 0)
                sql += " and   BodyLengthId=" + p5;
            sql += " order by RegisterFlag, JobNo desc";
            var jobListPage = new JobinfoPageModel
            {
                BaseInfos = GswTblBaseInfo.Fetch("where CustomerId=19 and (FunctionId=59 or FunctionId=60 or FunctionId=66 or  FunctionId=67 or  FunctionId=68) order by FunctionId,TypeNo"),
                JobInfos = db.Page<GswTblJobInfo>(pageIndex, 10, sql)
            };
            ViewBag.p1 = p1;
            ViewBag.p2 = p2;
            ViewBag.p3 = p3;
            ViewBag.p4 = p4;
            ViewBag.p5 = p5;
            return View(jobListPage);
        }
        // 职位详情
        public ActionResult JobDesc(int id)
        {
            ViewBag.msg = "";
            var qry = GswTblJobInfo.SingleOrDefault(id);
            qry.JobInfos = GswTblJobInfo.Fetch("select top 10 * from GswTblJobInfo where ParentId=19 and JobState=1 and delFlag=0 order by id desc");
            qry.Notices = GswTblContent.Fetch("select top 10 * from GswTblContent where FunctionId=79 and CustomerId=19 order by id desc ");
            qry.JobNews = GswTblContent.Fetch("select top 10 * from GswTblContent where (FunctionId=83 or FunctionId=82 ) and CustomerId=19 order by id desc ");
            return View(qry);
        }
        //报名登记
        public ActionResult JobRegister(int id)
        {
            if (Session["member"] == null)
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/JobDesc/" + id });
            var member = (GswTblMember)Session["member"];
            if (GswTblJobWork.Exists("JobId=" + id + " and MemberId=" + member.Id))
            {
                ModelState.AddModelError(string.Empty, "已报名");
                ViewBag.msg = "ex";
            }
            if (member.MemberPoint < 10)
            {
                ModelState.AddModelError(string.Empty, "积分不足");
                ViewBag.msg = "ex1";
            }
            try
            {
                if (ModelState.IsValid)
                {
                    var jobwork = new GswTblJobWork { RegDate = DateTime.Today, JobId = id, MemberId = member.Id };
                    jobwork.Insert();
                    GswTblJobInfo.Update("set RegisterNum=RegisterNum+1 where id=" + id);
                    GswTblJobInfo.Update("set RegisterFlag=1 where RegisterFlag>=JobNum");
                    var memberPoint = new GswTblMemberPoint
                    {
                        PointDate = DateTime.Today,
                        PointNum = -10,
                        UserId = member.Id,

                        PointSource = DateTime.Today.ToShortDateString() + "兼职报名"
                    };
                    memberPoint.Insert();
                    GswTblMember.Update("set MemberPoint=MemberPoint-10 where id=" + member.Id);
                    ViewBag.msg = "ok";
                }

            }
            catch (Exception e)
            {
                ViewBag.msg = "no";

            }
            var qry = GswTblJobInfo.SingleOrDefault(id);
            qry.JobInfos = GswTblJobInfo.Fetch("select top 10 * from GswTblJobInfo where ParentId=19 and JobState=1 and delFlag=0 order by id desc");
            qry.Notices = GswTblContent.Fetch("select top 10 * from GswTblContent where FunctionId=79 and CustomerId=19 order by id desc ");
            qry.JobNews = GswTblContent.Fetch("select top 10 * from GswTblContent where (FunctionId=83 or FunctionId=82 ) and CustomerId=19 order by id desc ");
            return View("JobDesc", qry);

        }
        //会员中心
        public ActionResult MemberCenter()
        {
            if (Session["member"] == null)
            {
                ViewBag.returl = "/SunJob/MemberCenter";
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberCenter" });
            }
            var member = (GswTblMember)Session["member"];
            return View(member);
        }

        //会员积分
        public ActionResult MemberPoint(int id,int pageIndex=1)
        {
            if (Session["member"] == null)
            {
                ViewBag.returl = "/SunJob/MemberPoint";
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberPoint/" + id });
            }
            var member = (GswTblMember)Session["member"];
            member.MemberPoints = db.Page<GswTblMemberPoint>(pageIndex, 10, "select * from GswTblMemberPoint where UserId=" + id); //GswTblMemberPoint.Fetch("where UserId=" + id);
            return View(member);
        }
        //录取通知
        public ActionResult UserMsg(int id,int pageIndex=1)
        {
            if (Session["member"] == null)
            {
                ViewBag.returl = "/SunJob/UserMsg";
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/UserMsg/" + id });
            }
            var member = (GswTblMember)Session["member"];
            member.JobWorkAlls = db.Page<GswViewJobWorkAll>(pageIndex,10,"where MemberId=" + id + " and AcceptedFlag=1 order by regdate desc");
            return View(member);
        }
        //领薪通知
        public ActionResult UserMsg1(int id,int pageIndex=1)
        {
            if (Session["member"] == null)
            {
                ViewBag.returl = "/SunJob/UserMsg1";
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/UserMsg1/" + id });
            }
            var member = (GswTblMember)Session["member"];
            member.JobWorkAlls = db.Page<GswViewJobWorkAll>(pageIndex, 10, "where MemberId=" + id + " and PayNoticeFlag=1 order by PayNoticeDate desc");
            return View(member);
        }
        //我的工作
        public ActionResult MemberJob(int id,int pageIndex=1)
        {
            if (Session["member"] == null)
            {
                ViewBag.returl = "/SunJob/MemberJob";
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberJob/" + id });
            }
            var member = (GswTblMember)Session["member"];
            member.JobWorkAlls = db.Page<GswViewJobWorkAll>(pageIndex, 10, "where MemberId=" + id + "  order by regdate desc");
            //and AcceptedFlag=1
            return View(member);
        }

        public ActionResult MemberJobDelete(int id, int mid,int pageIndex=1)
        {
            if (Session["member"] == null)
            {
                ViewBag.returl = "/SunJob/MemberJob";
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberJob/" + mid });
            }
            var member = (GswTblMember)Session["member"];
            var work = GswTblJobWork.SingleOrDefault(id);
            int jid = work.JobId;
            GswTblJobInfo.Update("set RegisterNum=RegisterNum-1 where id=" + jid);
            GswTblJobInfo.Update("set RegisterNum=0 where RegisterNum<0");
            work.Delete();

            member.JobWorkAlls = db.Page<GswViewJobWorkAll>(pageIndex, 10, "where MemberId=" + mid + "  order by regdate desc");
            //and AcceptedFlag=1
            return View("MemberJob", member);
        }

        //我的时间
        public ActionResult MemberTime(int id)
        {
            if (Session["member"] == null)
            {
                ViewBag.returl = "/SunJob/MemberTime";
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberTime/" + id });
            }
            var member = (GswTblMember)Session["member"];
            member.MemberTimes = GswTblMemberTime.Fetch("where MemberId=" + id + "  order by id");
            if (member.MemberTimes.Count == 0)
            {
                var mt = new GswTblMemberTime { WorkTime = "08：00-10：00", MemberId = id, LastDate = DateTime.Today };
                mt.Insert();
                mt.WorkTime = "10：00-12：00";
                mt.MemberId = id;
                mt.LastDate = DateTime.Today;
                mt.Insert();
                mt.WorkTime = "12：00-14：00";
                mt.MemberId = id;
                mt.LastDate = DateTime.Today;
                mt.Insert();
                mt.WorkTime = "14：00-18：00";
                mt.MemberId = id;
                mt.LastDate = DateTime.Today;
                mt.Insert();
                mt.WorkTime = "18：00—20：00";
                mt.MemberId = id;
                mt.LastDate = DateTime.Today;
                mt.Insert();
                member.MemberTimes = GswTblMemberTime.Fetch("where MemberId=" + id + "  order by id");
            }
            //and AcceptedFlag=1
            return View(member);
        }
        [HttpPost]
        public ActionResult MemberTime(int id, FormCollection c)
        {
            if (Session["member"] == null)
            {
                ViewBag.returl = "/SunJob/MemberTime";
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberTime/" + id });
            }
            var member = (GswTblMember)Session["member"];
            member.MemberTimes = GswTblMemberTime.Fetch("where MemberId=" + id + "  order by id");
            foreach (var item in member.MemberTimes)
            {
                int d1 = InputValid.StrToInt(c["chk1" + item.Id], 0);
                 int d2 = InputValid.StrToInt(c["chk2" + item.Id], 0);
                 int d3 = InputValid.StrToInt(c["chk3" + item.Id], 0);
                 int d4 = InputValid.StrToInt(c["chk4" + item.Id], 0);
                 int d5 = InputValid.StrToInt(c["chk5" + item.Id], 0);
                 int d6 = InputValid.StrToInt(c["chk6" + item.Id], 0);
                 int d7= InputValid.StrToInt(c["chk7" + item.Id], 0);
                GswTblMemberTime.Update("set D1=" + d1 + ",D2=" + d2 + ",D3=" + d3 + ",D4=" + d4 + ",D5=" + d5 + ",D6=" +
                                        d6 + ",D7=" + d7 + ",LastDate='" + DateTime.Today.ToShortDateString() +
                                        "' where id=" + item.Id);
            }
            member.MemberTimes = GswTblMemberTime.Fetch("where MemberId=" + id + "  order by id");
            //and AcceptedFlag=1
            return View(member);
        }

        public ActionResult MemberHelpAdd(int id)
        {
            if (Session["member"] == null)
            {
                ViewBag.returl = "/SunJob/MemberHelpAdd";
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberHelpAdd/" + id });
            }
            var member = (GswTblMember)Session["member"];
            return View(member);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult MemberHelpAdd(int id,FormCollection c)
        {
            if (Session["member"] == null)
            {
                ViewBag.returl = "/SunJob/MemberHelpAdd";
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberHelpAdd/" + id });
            }
            var member = (GswTblMember)Session["member"];
            if (string.IsNullOrEmpty(c["HelpTitle"]))
            {
                ViewBag.msg = "内容不允许为空";
                ModelState.AddModelError(string.Empty, "内容不允许为空");
            }
                
            try
            {
                if (ModelState.IsValid)
                {
                    int sno = GswTblSno.GetSnoByMonth("mh");
                    string jobno = "H" + InputValid.Right(DateTime.Today.Year.ToString(), 2) +
                                   DateTime.Today.Month.ToString("D2") + DateTime.Today.Day.ToString("D2") +
                                   sno.ToString("D2");
                   
                   
                    var mh = new GswTblMemberHelp
                    {
                        PubDate = DateTime.Today,
                        EndDate = DateTime.Parse(c["EndDate"]),
                        MemberId = id,
                        HelpTitle = c["HelpTitle"],
                        HelpType = c["HelpType"],
                        HelpNo = jobno
                        
                    };
                    mh.Insert();
                    GswTblSno.Update("set sno=@0,curyear=@1,curmonth=@2 where keycode=@3", sno + 1, DateTime.Today.Year, DateTime.Today.Month, "mh");
                    ViewBag.msg = "您的需求已成功发布";
                }
            }
            catch (Exception e)
            {

                ModelState.AddModelError(string.Empty, e.Message);
            }

            return View(member);
        }

        public ActionResult MemberHelpList(int id,int pageIndex=1)
        {
            if (Session["member"] == null)
            {
                ViewBag.returl = "/SunJob/MemberHelpAdd";
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberHelpList/" + id });
            }
            var member = (GswTblMember)Session["member"];
            member.MemberHelps = db.Page<GswTblMemberHelp>(pageIndex,10,"where MemberId=" + id + " order by PubDate");// GswTblMemberHelp.Fetch("where MemberId=" + id + " order by PubDate");
            foreach (var item in member.MemberHelps.Items.Where(item => item.SupplierId > 0))
            {
                item.MemberInfo = GswTblMember.SingleOrDefault(item.SupplierId);
            }
            return View(member);
        }
        public ActionResult MemberOrder(int id,int pageIndex=1)
        {
            if (Session["member"] == null)
            {
                ViewBag.returl = "/SunJob/MemberHelpAdd";
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberHelpList/" + id });
            }
            var member = (GswTblMember)Session["member"];
            member.MemberHelps = db.Page<GswTblMemberHelp>(pageIndex, 10, "where SupplierId=" + id + " order by PubDate"); //GswTblMemberHelp.Fetch("where SupplierId=" + id + " order by PubDate");
            foreach (var item in member.MemberHelps.Items.Where(item => item.SupplierId > 0))
            {
                item.MemberInfo = GswTblMember.SingleOrDefault(item.MemberId);
            }
            return View(member);
        }
        //注册
        public ActionResult Register()
        {
            var memberuser = new GswTblMember
            {
                BaseInfos = GswTblBaseInfo.Fetch("where CustomerId=19 and (FunctionId=76 or FunctionId=77 or FunctionId=78)")
            };
            return View(memberuser);
        }

        [HttpPost]
        public ActionResult Register(FormCollection c)
        {

            if (string.IsNullOrEmpty(InputValid.DelSqlStr(c["MemberUserName"])))
                ModelState.AddModelError("", "请输入正确的用户名");
            if (string.IsNullOrEmpty(InputValid.DelSqlStr(c["MemberPwd"])))
                ModelState.AddModelError("", "请输入正确的密码字符串");

            if (GswTblMember.Exists("MemberUserName='" + InputValid.DelSqlStr(c["MemberUserName"]) + "'"))
                ModelState.AddModelError("", "用户名已经存在");

            var memberuser = new GswTblMember();
            memberuser.MemberType = c["MemberType"];
            memberuser.MemberTypeId = 0;
            memberuser.MemberName = c["MemberName"];
            memberuser.MemberNickname = "";
            memberuser.MemberCardNo = "";
            memberuser.SchoolName = c["SchoolName"];
            memberuser.Major = c["Major"];
            memberuser.EnterSchoolYear = c["EnterSchoolYear"];
            memberuser.Education = c["Education"];
            memberuser.Gender = c["Gender"];
            memberuser.BodyLength = InputValid.StrToDecimal(c["BodyLength"], 0);
            memberuser.Birthday = DateTime.Today;
            memberuser.MemberPic = "";
            memberuser.MemberTel = c["MemberTel"];
            memberuser.MemberQQ = c["MemberQQ"];
            memberuser.MemberEmail = c["MemberEmail"];
            memberuser.MemberUserName = c["MemberUserName"];
            memberuser.RegDate = DateTime.Today;
            memberuser.ParentId = 19;
            memberuser.MemberPoint = 50;
            memberuser.PwdSalt = EncryptPwd.GenerateSalt();
            memberuser.PwdHash = EncryptPwd.EncodePassword(c["MemberPwd"].Trim(), memberuser.PwdSalt);
            memberuser.MemberDesc = c["MemberDesc"];
            memberuser.AuditFlag = c["MemberType"].Trim() == "无卡会员" ? 1 : 0;
            if (!string.IsNullOrEmpty(memberuser.Gender))
            {
                memberuser.MemberPic = memberuser.Gender.Trim() == "男" ? "/Assets/img/0.jpg" : "/Assets/img/1.jpg";

            }

            //int saveFlag = 0;
            try
            {
                if (ModelState.IsValid)
                {
                    memberuser.Insert();
                    var memberPoint = new GswTblMemberPoint
                    {
                        PointDate = DateTime.Today,
                        PointNum = 50,
                        UserId = memberuser.Id,
                        PointSource = "注册赠送积分"
                    };
                    memberPoint.Insert();
                    Session["loginstate"] = "ok";
                    Session["member"] = memberuser;
                }


            }
            catch (Exception e)
            {

                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (Session["loginstate"].ToString() == "ok")
            {
                return RedirectToAction("MemberCenter", "SunJob");

            }
            return View(memberuser);
        }
        //用户信息修改
        public ActionResult UserInfoEdit(int id)
        {

            var member = GswTblMember.SingleOrDefault(id);
            member.BaseInfos =
                GswTblBaseInfo.Fetch("where CustomerId=19 and (FunctionId=76 or FunctionId=77 or FunctionId=78)");
            return View(member);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult UserInfoEdit(FormCollection c, int id)
        {
            var memberuser = GswTblMember.SingleOrDefault(id);
            memberuser.MemberName = c["MemberName"];
            memberuser.MemberNickname = "";
            memberuser.SchoolName = c["SchoolName"];
            memberuser.Major = c["Major"];
            memberuser.EnterSchoolYear = c["EnterSchoolYear"];
            memberuser.Education = c["Education"];
            memberuser.Gender = c["Gender"];
            memberuser.BodyLength = InputValid.StrToDecimal(c["BodyLength"], 0);
            memberuser.MemberPic = c["MemberPic"];
            memberuser.MemberTel = c["MemberTel"];
            memberuser.MemberQQ = c["MemberQQ"];
            memberuser.MemberEmail = c["MemberEmail"];
            memberuser.MemberBrief = c["MemberBrief"];
            // memberuser.RegDate = DateTime.Today;
            //memberuser.ParentId = 19;
            //memberuser.MemberPoint = 50;
            //memberuser.PwdSalt = EncryptPwd.GenerateSalt();
            //memberuser.PwdHash = EncryptPwd.EncodePassword(c["MemberPwd"].Trim(), memberuser.PwdSalt);
            memberuser.MemberDesc = "";
            //memberuser.AuditFlag = c["MemberType"].Trim() == "无卡会员" ? 1 : 0;
            try
            {
                if (ModelState.IsValid)
                {
                    memberuser.Update();
                    Session["loginstate"] = "ok";
                    Session["member"] = memberuser;
                }


            }
            catch (Exception e)
            {

                ModelState.AddModelError(string.Empty, e.Message);
            }
            memberuser.BaseInfos =
              GswTblBaseInfo.Fetch("where CustomerId=19 and (FunctionId=76 or FunctionId=77 or FunctionId=78)");
            return View(memberuser);
        }
        //密码修改
        public ActionResult ChangePassword(int id)
        {

            var member = GswTblMember.SingleOrDefault(id);
            return View(member);

        }
        [HttpPost]
        public ActionResult ChangePassword(int id, FormCollection c)
        {

            var member = GswTblMember.SingleOrDefault(id);
            var sh1Pwd = EncryptPwd.EncodePassword(c["OldPwd"], member.PwdSalt);
            if (member.PwdHash.Trim() != sh1Pwd)
                ModelState.AddModelError(string.Empty, "原密码不正确");
            if (string.IsNullOrEmpty(InputValid.DelSqlStr(c["NewPwd"])))
                ModelState.AddModelError(string.Empty, "请输入正确的新密码");
            try
            {
                if (ModelState.IsValid)
                {

                    var pwdSalt = EncryptPwd.GenerateSalt();
                    var pwdHash = EncryptPwd.EncodePassword(c["NewPwd"].Trim(), pwdSalt);
                    member.PwdSalt = pwdSalt;
                    member.PwdHash = pwdHash;
                    member.Update();
                }
                ViewBag.editok = "ok";
            }
            catch (Exception e)
            {

                ModelState.AddModelError(string.Empty, e.Message);
            }
            member.BaseInfos =
              GswTblBaseInfo.Fetch("where CustomerId=19 and (FunctionId=76 or FunctionId=77 or FunctionId=78)");
            return View(member);

        }
        //登录
        public ActionResult SignIn(string returnUrl = "")
        {
            ViewBag.returl = returnUrl;
            if (Session["member"] == null) return View();
            if (!string.IsNullOrEmpty(returnUrl))
                return Redirect(returnUrl);
            return RedirectToAction("Index", "Home");

            //
        }
        [HttpPost]
        public ActionResult SignIn(FormCollection c)
        {
            if (string.IsNullOrEmpty(InputValid.DelSqlStr(c["username"])))
                ModelState.AddModelError(string.Empty, "请输入正确的用户名");
            if (string.IsNullOrEmpty(InputValid.DelSqlStr(c["pwd"])))
                ModelState.AddModelError(string.Empty, "请输入正确的密码");
            var member =
                GswTblMember.SingleOrDefault("where MemberUserName='" + InputValid.DelSqlStr(c["username"]) + "'");
            if (member == null)
                ModelState.AddModelError(string.Empty, "用户名不存在");
            else
            {
                string sh1Pwd = EncryptPwd.EncodePassword(c["pwd"], member.PwdSalt);
                if (member.PwdHash.Trim() == sh1Pwd)
                {
                    var qry =
                        GswTblMemberPoint.SingleOrDefault("where PointNum>0 and UserId=" + member.Id + " and PointDate='" +
                                                     DateTime.Today.ToShortDateString() + "'");
                    int pnum = 0;
                    if (member.MemberType.Trim() == "无卡会员")
                        pnum = 1;
                    if (member.MemberType.Trim() == "优卡会员")
                        pnum = 3;
                    if (member.MemberType.Trim() == "钻石卡会员")
                        pnum = 5;
                    if (qry == null)
                    {
                        var memberPoint = new GswTblMemberPoint
                        {
                            PointDate = DateTime.Today,
                            PointNum = pnum,
                            UserId = member.Id,
                           
                            PointSource = DateTime.Today.ToShortDateString() + "登录积分"
                        };
                        memberPoint.Insert();
                        GswTblMember.Update("set MemberPoint=MemberPoint+"+pnum+" where id=" + member.Id);
                    }

                    Session["member"] = member;
                    Session["loginstate"] = "ok";
                    Session["TrueName"] = member.MemberName;
                    Session["UserId"] = member.Id;
                    Session["UserType"] = member.MemberType;
                    if (string.IsNullOrEmpty(c["returl"]))
                        return RedirectToAction("Index", "Home");
                    return Redirect(c["returl"]);
                }
                ModelState.AddModelError(string.Empty, "请输入正确的密码");
            }
            ViewBag.returl = c["returl"];
            return View();
        }
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            System.Web.HttpContext.Current.Session["member"] = null;
            System.Web.HttpContext.Current.Session["loginstate"] = null;
            System.Web.HttpContext.Current.Session["TrueName"] = null;
            // GswTblUser.LoginOut();
            return RedirectToAction("Index", "Home");
        }
        //包车预订
        public ActionResult CharteredBus()
        {
            var qry = GswTblBusInfo.Fetch("where ParentId=19 and EndDate>='" + DateTime.Today.ToShortDateString() + "' order by StartDate");
            return View(qry);
        }

        public ActionResult BusDesc(int id)
        {
            var qry = GswTblBusInfo.SingleOrDefault(id);
            qry.JobInfos = GswTblJobInfo.Fetch("select top 10 * from GswTblJobInfo where ParentId=19 and JobState=1 and delFlag=0 order by id desc");
            qry.Notices = GswTblContent.Fetch("select top 10 * from GswTblContent where FunctionId=79 and CustomerId=19 order by id desc ");
            qry.PartNews = GswTblContent.Fetch("select top 10 * from GswTblContent where FunctionId=85  and CustomerId=19 order by id desc ");
            return View(qry);
        }

        public ActionResult BusRegister(int id)
        {
            if (Session["member"] == null)
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/BusDesc/" + id });
            var member = (GswTblMember)Session["member"];
            if (GswTblBusBook.Exists("BusId=" + id + " and MemberId=" + member.Id))
            {
                ModelState.AddModelError(string.Empty, "已报名");
                ViewBag.msg = "ex";
            }
            try
            {
                if (ModelState.IsValid)
                {
                    var busbook = new GswTblBusBook { BookDate = DateTime.Today, BookTime = DateTime.Now.ToShortTimeString(), BusId = id, MemberId = member.Id, BookNum = 1 };
                    busbook.Insert();
                    GswTblBusInfo.Update("set RegNum=RegNum+1 where id=" + id);
                    ViewBag.msg = "ok";
                }

            }
            catch (Exception e)
            {
                ViewBag.msg = "no";

            }
            var qry = GswTblBusInfo.SingleOrDefault(id);
            qry.JobInfos = GswTblJobInfo.Fetch("select top 10 * from GswTblJobInfo where ParentId=19 and JobState=1 and delFlag=0 order by id desc");
            qry.Notices = GswTblContent.Fetch("select top 10 * from GswTblContent where FunctionId=79 and CustomerId=19 order by id desc ");
            qry.PartNews = GswTblContent.Fetch("select top 10 * from GswTblContent where FunctionId=85  and CustomerId=19 order by id desc ");
            return View("BusDesc", qry);
        }

        //就业培训
        public ActionResult Train(int id = 82,int pageIndex=1)
        {
            var otherId = id == 82 ? 83 : 82;
            var pageModel = new TrainPageModel
            {
                JobInfos =
                    GswTblJobInfo.Fetch(
                        "select top 10 * from GswTblJobInfo where ParentId=19 and JobState=1 and delFlag=0 order by id desc"),
                Contents = db.Page<GswTblContent>(pageIndex, 10, "select  * from  GswTblContent where FunctionId=" + id + " and CustomerId=19 order by id desc"),
                //GswTblContent.Fetch(" where FunctionId=" + id + " and CustomerId=19 order by id desc"),
                SideContents =
                    GswTblContent.Fetch("select top 10 * from GswTblContent where FunctionId=" + otherId +
                                        "  and CustomerId=19 order by id desc "),
                PicList = GswTblContent.Fetch("select top 5 * from GswTblContent where FunctionId=82 and CustomerId=19 and NewFlag=1 order by id desc"),
                PageTitle = id == 82 ? "阳光培训" : "阳光就业",
                FunctionId = id


            };
            //var test = 1 % 5;
            //var test0 = 2 % 5;
            //var test1 = 3 % 5;
            //var test2 = 4 % 5;
            //var test3 = 5 % 5;
            return View(pageModel);
        }

        public ActionResult TrainDesc(int id)
        {
            var qry = GswTblContent.SingleOrDefault(id);
            var otherId = qry.FunctionId == 82 ? 83 : 82;
            qry.ContentList0 = GswTblContent.Fetch("where CustomerId=19 and Id<>" + id + " and FunctionId=" + otherId + " order by id");
            qry.JobInfos = GswTblJobInfo.Fetch(
                "select top 10 * from GswTblJobInfo where ParentId=19 and JobState=1 and delFlag=0 order by id desc");
            return View(qry);

        }
        //校园分享
        public ActionResult SchoolShare(int id = 0, int typeId = 0, int stypeId = 0)
        {
            var sql = " where FunctionId=90 and CustomerId=19 ";
            if (typeId == 0 && stypeId == 0)
                sql += " and NewFlag=1";
            else
            {
                if (typeId > 0)
                    sql += " and MasterId=" + typeId;
                if (stypeId > 0)
                    sql += " and ContentTypeId=" + stypeId;
            }
            sql += " order by id desc";
            var pageModel = new SchoolSharePageModel
            {
                ProTypes = GswTblContent.GetShareTypes(19),
                Contents = GswViewContent.Fetch(sql)
            };

            return View(pageModel);
        }

        public ActionResult SchoolShareDesc(int id)
        {
            var qry = GswTblContent.SingleOrDefault(id);

            qry.ContentList0 = GswTblContent.Fetch("select top 10 * from GswTblContent where CustomerId=19 and Id<>" + id + " and FunctionId=90 and NewFlag=1 order by id");
            qry.JobInfos = GswTblJobInfo.Fetch(
                "select top 10 * from GswTblJobInfo where ParentId=19 and JobState=1 and delFlag=0 order by id desc");
            return View(qry);
        }
        //精彩一刻
        public ActionResult HappyTime()
        {
            var htlist = GswTblContent.Fetch(" where FunctionId=84 and CustomerId=19 order by id desc");
            return View(htlist);
        }


        public ActionResult HaypyTimeDesc(int id)
        {
            var qry = GswTblContent.SingleOrDefault(id);

            qry.ContentList0 = GswTblContent.Fetch("select top 10 * from GswTblContent where CustomerId=19 and Id<>" + id + " and FunctionId=85  order by id desc");
            qry.JobInfos = GswTblJobInfo.Fetch(
                "select top 10 * from GswTblJobInfo where ParentId=19 and JobState=1 and delFlag=0 order by id desc");
            return View(qry);
        }
        //主题活动
        public ActionResult ThemedEvents()
        {
            var telist = GswTblContent.Fetch(" where FunctionId=85 and CustomerId=19 order by id desc");
            return View(telist);

        }
        public ActionResult ThemedEventsDesc(int id)
        {
            var qry = GswTblContent.SingleOrDefault(id);

            qry.ContentList0 = GswTblContent.Fetch("select top 10 * from GswTblContent where CustomerId=19 and Id<>" + id + " and FunctionId=85  order by id desc");
            qry.JobInfos = GswTblJobInfo.Fetch(
                "select top 10 * from GswTblJobInfo where ParentId=19 and JobState=1 and delFlag=0 order by id desc");
            return View(qry);
        }
        //学员风采
        public ActionResult MemberNews()
        {
            var telist = GswTblContent.Fetch(" where FunctionId=102 and CustomerId=19 order by id desc");
            return View(telist);
        }
        public ActionResult MemberNewsDesc(int id)
        {
            var qry = GswTblContent.SingleOrDefault(id);
            qry.ContentList0 = GswTblContent.Fetch("select top 10 * from GswTblContent where CustomerId=19 and Id<>" + id + " and FunctionId=90 and NewFlag=1 order by id");
            qry.JobInfos = GswTblJobInfo.Fetch(
                "select top 10 * from GswTblJobInfo where ParentId=19 and JobState=1 and delFlag=0 order by id desc");
            return View(qry);
        }
        //公告通知
        public ActionResult Notice(int id=0,int pageIndex=1)
        {

            var pageModel = new TrainPageModel
            {
                JobInfos =
                    GswTblJobInfo.Fetch(
                        "select top 20 * from GswTblJobInfo where ParentId=19 and JobState=1 and delFlag=0 order by id desc"),
                Contents = db.Page<GswTblContent>(pageIndex, 20, "select  * from GswTblContent where FunctionId=79 and CustomerId=19 order by id desc"),
                //SideContents =
                //    GswTblContent.Fetch("select top 10 * from GswTblContent where FunctionId=" + otherId +
                //                        "  and CustomerId=19 order by id desc "),



            };

            return View(pageModel);
        }

        public ActionResult NoticeDesc(int id)
        {
            var qry = GswTblContent.SingleOrDefault(id);

            qry.JobInfos = GswTblJobInfo.Fetch(
                "select top 20 * from GswTblJobInfo where ParentId=19 and JobState=1 and delFlag=0 order by id desc");
            return View(qry);
        }
        public ActionResult Tieba(int id=0,int pageIndex=1)
        {
            //var qry = GswViewTieba.Fetch("where 0=0 order by PubDate desc");
            
            var qry=
            db.Page<GswViewTieba>(pageIndex, 10, " select * from GswViewTieba where 0=0 order by PubDate desc");
            return View(qry);
        }
        [HttpPost]
        public ActionResult TiebaPost(FormCollection c)
        {

            if (Session["member"] == null)
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/Tieba" });
            var member = (GswTblMember)Session["member"];
            if (string.IsNullOrEmpty(c["txttitle"]))
                ModelState.AddModelError(string.Empty, "主题不能为空");
            if (string.IsNullOrEmpty(c["txtcontent"]))
                ModelState.AddModelError(string.Empty, "内容不能为空");

            try
            {
                if (ModelState.IsValid)
                {
                    var tieba = new GswTblTieba
                    {
                        Title = c["txttitle"],
                        PubContent = c["txtcontent"],
                        PubDate = DateTime.Today,
                        PubTime = DateTime.Now.ToShortTimeString(),
                        UserId = member.Id,
                        ParentId = 19


                    };
                    tieba.Insert();
                }
            }
            catch (Exception e)
            {

                ModelState.AddModelError(string.Empty, e.Message);
            }
            return RedirectToAction("Tieba", "SunJob");
            //var qry = GswViewTieba.Fetch("where 0=0 order by PubDate desc");
            //return View("Tieba",qry);
        }

        public ActionResult TiebaDesc(int id)
        {
            GswTblTieba.Update("set VisitNum=VisitNum+1 where id=" + id);
            var qry = GswViewTieba.SingleOrDefault(id);
            qry.TiebaReplies = GswViewTiebaReply.Fetch("where MasterId=" + id + " order by id desc");
            return View(qry);

        }
        [HttpPost]
        public ActionResult TiebaReply(int id, FormCollection c)
        {
            if (Session["member"] == null)
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/TiebaDesc/" + id });
            var member = (GswTblMember)Session["member"];
            if (string.IsNullOrEmpty(c["content"]))
                ModelState.AddModelError(string.Empty, "内容不能为空");
            try
            {
                if (ModelState.IsValid)
                {
                    var tieReply = new GswTblTiebaReply
                    {
                        MasterId = id,
                        ReplyContent = c["content"],
                        ReplyDate = DateTime.Today,
                        ReplyTime = DateTime.Now.ToShortTimeString(),
                        ReplyUserId = member.Id
                    };
                    tieReply.Insert();
                    GswTblTieba.Update("set ReplyNum=ReplyNum+1 where id=" + id);
                }
            }
            catch (Exception e)
            {

                ModelState.AddModelError(string.Empty, e.Message);
            }
            return RedirectToAction("TiebaDesc", "SunJob", new { id });
        }

        public ActionResult SchoolHelp(int id=0,string p="",int pageIndex=1)
        {
            var sql = "select * from GswViewMemberHelper where AuditFlag=1 and HelpFlag=0";
            if(!string.IsNullOrEmpty(p))
                sql += "  and HelpType='"+p+"'";
            sql += " order by id desc";
            var qry = db.Page<GswViewMemberHelper>(pageIndex, 10, sql);
               // GswViewMemberHelper.Fetch(sql);
            return View(qry);
        }

        public ActionResult SchoolHelpOrder(int id)
        {
            if (Session["member"] == null)
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/SchoolHelpOrder/" + id });
            var mh = GswViewMemberHelper.SingleOrDefault(id);
            mh.JobInfos = GswTblJobInfo.Fetch("select top 10 * from GswTblJobInfo where ParentId=19 and JobState=1 and delFlag=0 order by id desc");
            var member = (GswTblMember)Session["member"];
            if (member.MemberPoint < 5)
            {
                ViewBag.msg = "积分不足,抢单失败";
                return View(mh);
            }
            if (mh.MemberId == member.Id)
            {
                ViewBag.msg = "不允许自娱自乐";
                return View(mh);
            }
             
            if (mh.HelpFlag == 0)
            {
                var memberPoint = new GswTblMemberPoint
                {
                    PointDate = DateTime.Today,
                    PointNum = -5,
                    UserId = member.Id,

                    PointSource = DateTime.Today.ToShortDateString() + "抢单"
                };
                memberPoint.Insert();
                GswTblMember.Update("set MemberPoint=MemberPoint-5 where id=" + member.Id);
                GswTblMemberHelp.Update("set HelpFlag=1,OrderDate='" + DateTime.Today.ToShortDateString() + "',SupplierId=" +
                                    member.Id + " where Id=" + id);
                ViewBag.msg = "恭喜你,抢单成功";
            }
            else
            {
                ViewBag.msg = "对不起,您慢了半步";
            }
            return View(mh);

        }

        public ActionResult MemberJobpub(int id)
        {
            if (Session["member"] == null)
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberJobpub/" + id });
            var member = GswTblMember.SingleOrDefault(id);
            member.JobInfos = GswTblJobInfo.Fetch("where MemberId=" + id + " order by PubDate desc");

            return View(member);

        }

        public ActionResult MemberJobpubDel(int id, int jobid)
        {
            if (Session["member"] == null)
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberJobpub/" + id });
            GswTblJobInfo.Delete(jobid);
            var member = GswTblMember.SingleOrDefault(id);
            member.JobInfos = GswTblJobInfo.Fetch("where MemberId=" + id + " order by PubDate desc");

            return View("MemberJobpub", member);
        }

        public ActionResult MemberJobpubAdd(int id, int jobid=0)
        {
            if (Session["member"] == null)
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberJobpub/" + id });
            var member = GswTblMember.SingleOrDefault(id);
            member.JobInfo = jobid == 0 ? new GswTblJobInfo{PubDate = DateTime.Today,EndDate = DateTime.Today} : GswTblJobInfo.SingleOrDefault(jobid);
            member.BaseInfos = GswTblBaseInfo.Fetch("where CustomerId=19  and (FunctionId=59  or FunctionId=60 or FunctionId=66 or FunctionId=67 or FunctionId=68 ) order by FunctionName,TypeNo");
                                     
            return View(member);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult MemberJobpubAdd(int id, FormCollection c,int jobid = 0)
        {
            if (Session["member"] == null)
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberJobpub/" + id });
            var member = GswTblMember.SingleOrDefault(id);
            member.JobInfo = jobid == 0 ? new GswTblJobInfo { PubDate = DateTime.Today, EndDate = DateTime.Today } : GswTblJobInfo.SingleOrDefault(jobid);
            if (InputValid.StrToInt(c["JobTypeId"], 0) == 0)
                ModelState.AddModelError("", "请选择职位类别");
            var jobInfo = jobid == 0 ? new GswTblJobInfo() : GswTblJobInfo.SingleOrDefault(jobid);

            var baseInfos = GswTblBaseInfo.Fetch("where CustomerId=19  and (FunctionId=59  or FunctionId=60 or FunctionId=66 or FunctionId=67 or FunctionId=68 ) order by FunctionName,TypeNo");
           // var d = c["PubDate"];
            jobInfo.PubDate = DateTime.Parse(c["PubDate"]);

            jobInfo.EndDate = DateTime.Parse(c["EndDate"]);
            //jobInfo.JobNo = c["JobNo"];
            jobInfo.JobTitle = c["JobTitle"];
            jobInfo.JobNum = InputValid.StrToInt(c["JobNum"], 0);
            jobInfo.WorkDate = c["WorkDate"];
            jobInfo.WorkTime = c["WorkTime"];
            jobInfo.Workplace = c["Workplace"];
            jobInfo.Salary = c["Salary"];
            jobInfo.CustomerId =0;
            jobInfo.CusShortName = c["CusShortName"];
            jobInfo.CustomerAlias ="";
            jobInfo.JobTypeId = InputValid.StrToInt(c["JobTypeId"], 0);
            jobInfo.JobTypeName = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["JobTypeId"], 0));
            jobInfo.WorkTypeId = InputValid.StrToInt(c["WorkTypeId"], 0);
            jobInfo.WorkTypeName = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["WorkTypeId"], 0));
            jobInfo.PayTypeId = InputValid.StrToInt(c["PayTypeId"], 0);
            jobInfo.PayTypeName = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["PayTypeId"], 0));
            jobInfo.BodyLengthId = InputValid.StrToInt(c["BodyLengthId"], 0);
            jobInfo.BodyLength = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["BodyLengthId"], 0));
            jobInfo.GenderId = InputValid.StrToInt(c["GenderId"], 0);
            jobInfo.GenderName = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["GenderId"], 0));
            jobInfo.RegRankId = 0;
            jobInfo.RegRank = "";
            jobInfo.JobInterview = c["JobInterview"];
            jobInfo.JobDesc = c["JobDesc"];
            jobInfo.ParentId = 19;
            jobInfo.JobPic ="";
            jobInfo.ReFlag = 0;
            jobInfo.HotFlag = 0;
            jobInfo.SourceFlag = 1;
            jobInfo.MemberId = member.Id;
            jobInfo.JobState = 0;
            jobInfo.DelFlag = 0;
           
            try
            {
                if (ModelState.IsValid)
                {

                    if (jobid == 0)
                    {

                        if (string.IsNullOrEmpty(c["JobNo"]))
                        {
                            int sno = GswTblSno.GetSnoByMonth("job");
                            string jobno = "P" + InputValid.Right(DateTime.Today.Year.ToString(), 2) +
                                           DateTime.Today.Month.ToString("D2") + DateTime.Today.Day.ToString("D2") +
                                           sno.ToString("D2");
                            GswTblSno.Update("set sno=@0,curyear=@1,curmonth=@2 where keycode=@3", sno + 1, DateTime.Today.Year, DateTime.Today.Month, "job");
                            jobInfo.JobNo = jobno;

                        }
                        else
                        {
                            jobInfo.JobNo = c["JobNo"];
                        }
                        jobInfo.PubTime = DateTime.Now.ToShortTimeString();
                        jobInfo.Insert();
                        jobInfo = new GswTblJobInfo { PubDate = DateTime.Today, EndDate = DateTime.Today, ParentId =19};

                    }
                    else
                    {
                        jobInfo.Update();
                    }
                    ViewBag.scode = "1";
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (ModelState.IsValid)
            {
                ViewBag.msg = "信息发布成功";
            }
            else
            {
                ViewBag.msg = "信息发布不成功";
            }
            member.BaseInfos = baseInfos;
            member.JobInfo = jobInfo;

            return View(member);
        }

        public ActionResult MemberJobpubView(int id, int jobid)
        {
            if (Session["member"] == null)
                return RedirectToAction("SignIn", "SunJob", new { returnUrl = "/SunJob/MemberJobpubView/" + id + "?jobid=" + jobid });
            var member = GswTblMember.SingleOrDefault(id);
            member.JobInfo = GswTblJobInfo.SingleOrDefault(jobid);
            //member.BaseInfos = GswTblBaseInfo.Fetch("where CustomerId=19 order by FunctionName,TypeNo");
            member.JobInfo.JobWorks = GswViewJobWork.Fetch("where JobId=" + jobid +
                                   " order by id");
            return View(member);
        }

        public ActionResult ForgetPwd()
        {
            ViewBag.flag = -1;
            return View();
        }
       [HttpPost]
        public ActionResult ForgetPwd(FormCollection c)
        {
            if (string.IsNullOrEmpty(c["username"]))
                ModelState.AddModelError("","请输入用户名");
            if (string.IsNullOrEmpty(c["MemberTel"]))
                ModelState.AddModelError("", "请输入电话号码");
            var s = DateTime.Today.Year+ DateTime.Today.Month.ToString("D2") + DateTime.Today.Day.ToString("D2");
                                           
           int flag = 0;
           try
           {
               if (ModelState.IsValid)
               {
                   var qry =
                       GswTblMember.FirstOrDefault("where MemberUserName='" + c["username"] + "' and MemberTel='" +
                                                   c["MemberTel"] + "'");
                   if (qry != null)
                   {
                       var  pwdSalt = GsnHelper.EncryptPwd.GenerateSalt();
                       var  pwdHash = GsnHelper.EncryptPwd.EncodePassword(s, pwdSalt);
                       GswTblMember.Update("set PwdHash='" + pwdHash + "', PwdSalt='" + pwdSalt +
                                           "' where MemberUserName='" + c["username"] + "' and MemberTel='" +
                                           c["MemberTel"] + "'");
                       flag = 1;

                   }
                   else
                   {
                       ModelState.AddModelError("", "用户名不存在或错误");
                   }
               }
           }
           catch (Exception e)
           {
               
               ModelState.AddModelError("",e.Message);
           }
           ViewBag.flag = flag;
           ViewBag.s = s;
            return View();
        }
    }
}
